package view.userinteface;

import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import view.controller.ControllerPrincipal;
import beans.BeanMarca;
import beans.BeanRodamiento;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class JInternalFrameBajaModificacionRodamiento extends javax.swing.JInternalFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1508594611515320556L;
	private JTextField txtCodigo;
	private JButton btnModificar;
	private JButton btnEliminar;
	private JLabel lblMarca;
	private JLabel lblTipo;
	private JLabel lblCodigo;
	private JButton btnBuscar;
	private JComboBox jComboBoxMarca;
	private JTextField txtTipo;
	private JScrollPane listadoRodamientos;
	private JTable tableRodamientos;
	private ControllerPrincipal controllerPrincipal;
	private static JInternalFrameBajaModificacionRodamiento instancia;
	private BeanRodamiento beanRodamiento = new BeanRodamiento();
	private JButton btnCancelar;
	private Map<Integer,List<String>> listaRodamientos = null;
	private DefaultTableModel jTable1Model = new DefaultTableModel();
	
	/**
	* Auto-generated main method to display this 
	* JInternalFrame inside a new JFrame.
	*/
	
	public static JInternalFrameBajaModificacionRodamiento getInstancia(){
		if (instancia==null){
			instancia = new JInternalFrameBajaModificacionRodamiento();
		}
		return instancia;
	}
	
	public JInternalFrameBajaModificacionRodamiento() {
		super();
		initGUI();
	}
	
	public void cargarMarcas (){
        List<BeanMarca> beanMarcas= controllerPrincipal.getInstancia().cargarComboMarcas();
        this.jComboBoxMarca.addItem("-- Seleccionar una Marca --");
        for (BeanMarca beanMarca:beanMarcas){
            this.jComboBoxMarca.addItem(beanMarca.toString());
        }
    }
	
	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(657, 529));
			this.setBounds(0, 0, 657, 529);
			setVisible(true);
			getContentPane().setLayout(null);
			{
				txtCodigo = new JTextField();
				getContentPane().add(txtCodigo);
				txtCodigo.setBounds(105, 12, 168, 23);
			}
			{
				txtTipo = new JTextField();
				getContentPane().add(txtTipo);
				txtTipo.setBounds(105, 65, 168, 23);
			}
			{
				ComboBoxModel jComboBoxMarcaModel = 
					new DefaultComboBoxModel(
							new String[] {});
				jComboBoxMarca = new JComboBox();
				getContentPane().add(jComboBoxMarca);
				jComboBoxMarca.setModel(jComboBoxMarcaModel);
				jComboBoxMarca.setBounds(450, 12, 193, 23);
			}
			{
				btnBuscar = new JButton();
				getContentPane().add(btnBuscar);
				btnBuscar.setText("Buscar");
				btnBuscar.setBounds(386, 65, 138, 23);
				btnBuscar.addActionListener(new ActionListener() {
				@SuppressWarnings("static-access")
				public void actionPerformed(ActionEvent evt) {
					beanRodamiento.setCodigo(txtCodigo.getText());
					beanRodamiento.setTipo(txtTipo.getText());
					cargarMarcaBusqueda(beanRodamiento);
					listaRodamientos = controllerPrincipal.getInstancia().getRodamientosByCriterio(beanRodamiento);
					jTable1Model.setRowCount(listaRodamientos.size());
					int i=0;
					Set<Integer> claves = listaRodamientos.keySet();
					for(Iterator<Integer> it = claves.iterator(); it.hasNext();i++){
						int cont=0;
						int aux = it.next();
						ArrayList<String> fila = (ArrayList<String>) listaRodamientos.get(aux);
						for(int j=0; j<5; j++){
							tableRodamientos.setValueAt(fila.get(cont), i, j);
							cont++;
						}
					}
					
				}
			});
			}
			{
				lblCodigo = new JLabel();
				getContentPane().add(lblCodigo);
				lblCodigo.setText("Codigo :");
				lblCodigo.setBounds(12, 15, 93, 16);
			}
			{
				lblTipo = new JLabel();
				getContentPane().add(lblTipo);
				lblTipo.setText("Tipo :");
				lblTipo.setBounds(27, 68, 73, 16);
			}
			{
				lblMarca = new JLabel();
				getContentPane().add(lblMarca);
				lblMarca.setText("Marca :");
				lblMarca.setBounds(370, 15, 79, 16);
			}
			{
				btnModificar = new JButton();
				getContentPane().add(btnModificar);
				btnModificar.setText("Modificar");
				btnModificar.setBounds(123, 438, 138, 23);
				btnModificar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
//						JInternalFrameModificacionRodamiento mr = new JInternalFrameModificacionRodamiento(beanRodamiento);
						beanRodamiento = obtenerRodamientoSeleccionado();
						JFrameModificacionRodamiento mr = new JFrameModificacionRodamiento(beanRodamiento);
						mr.setVisible(true);
					}

					
				});
			}
			{
				btnEliminar = new JButton();
				getContentPane().add(btnEliminar);
				btnEliminar.setText("Eliminar");
				btnEliminar.setBounds(363, 438, 138, 23);
				btnEliminar.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
//						JInternalFrameBajaRodamiento br = new JInternalFrameBajaRodamiento(beanRodamiento);
						beanRodamiento = obtenerRodamientoSeleccionado();
						JFrameBajaRodamiento br = new JFrameBajaRodamiento(beanRodamiento);
						br.setVisible(true);
					}
				});
			}
			{
				listadoRodamientos = new JScrollPane();
				getContentPane().add(listadoRodamientos);
				getContentPane().add(getJButton1());
				listadoRodamientos.setBounds(12, 100, 626, 332);
				listadoRodamientos.setEnabled(false);
				{
					tableRodamientos = new JTable(jTable1Model);
					jTable1Model.setColumnCount(5);
					jTable1Model.setColumnIdentifiers(new String[] { "Codigo","Tipo","Medida","Marca","Caracteristicas"});
					if (null != listaRodamientos)
						jTable1Model.setRowCount(listaRodamientos.size());
					else
						jTable1Model.setRowCount(0);
					listadoRodamientos.setViewportView(tableRodamientos);
					tableRodamientos.setBounds(12, 52, 550, 211);
					tableRodamientos.setPreferredSize(new java.awt.Dimension(623, 330));
				}
			}
			cargarMarcas();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private  BeanRodamiento obtenerRodamientoSeleccionado() {
		BeanRodamiento br = new BeanRodamiento();
		List<String> rodamSelected = rodamSelected();
		for (int i=0; i<5; i++){
			switch (i){
			//CODIGO
			case 0: br.setCodigo(rodamSelected.get(i));
			break;
			//TIPO
			case 1: br.setTipo(rodamSelected.get(i));
			break;
			//MEDIDA
			case 2: br.setMedida(rodamSelected.get(i));
			break;
			//MARCA
			case 3: BeanMarca m = new BeanMarca();
					String s = rodamSelected.get(i);
					int aux = s.indexOf(",");
					m.setDescripcion(s.substring(0, aux));
					m.setPais(s.substring(aux+2, s.length()));
//					m.setId(obtenerIdMarca((String)jComboBoxMarca.getSelectedItem()));
					br.setBeanMarca(m);
			break;
			//CARACTERISTICAS
			case 4: br.setCaracteristicas(rodamSelected.get(i));
			break;
			};
//			br.setId(obtenerIdRodamiento(rodamSelected));
		}
		return br;
	}
	
	private List<String> rodamSelected (){
		int fila = tableRodamientos.getSelectedRow();
		List<String> rodamSelected = new ArrayList<String>();
		for (int i=0; i<5; i++){
			rodamSelected.add((String)tableRodamientos.getValueAt(fila, i));
		}
		return rodamSelected;
	}
	
	private Integer obtenerIdRodamiento(List<String> rodamSelected){
		Set<Integer> claves = listaRodamientos.keySet();
		for (Iterator<Integer> it = claves.iterator();it.hasNext();){
			Integer aux = it.next();
			if(listaRodamientos.get(aux).equals(rodamSelected))
				return aux;
		}
		return 0;
	}
	
	private void cargarMarcaBusqueda (BeanRodamiento bRodamiento){
		BeanMarca bMarca = new BeanMarca();
		if (0 != jComboBoxMarca.getSelectedIndex()){
			String marca = ((String)jComboBoxMarca.getSelectedItem());
			int coma = marca.indexOf(",");
			bMarca.setDescripcion(marca.substring(0, coma));
			bMarca.setPais(marca.substring(coma+2, marca.length()));
//			bMarca.setId(jComboBoxMarca.getSelectedIndex());
		}
		bRodamiento.setBeanMarca(bMarca);
	}
	
//	public Integer obtenerIdMarca(String marcaSelected){
//		List<BeanMarca> beanMarcas = controllerPrincipal.getInstancia().cargarComboMarcas();
//		for (BeanMarca beanMarca : beanMarcas){
//			if (beanMarca.toString().equals(marcaSelected))
//				return beanMarca.getId();
//		}
//		return 0;
//	}
	
	private JButton getJButton1() {
		if(btnCancelar == null) {
			btnCancelar = new JButton();
			btnCancelar.setText("Cancelar");
			btnCancelar.setBounds(241, 472, 138, 23);
			btnCancelar.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent evt) {
					dispose();
				}
			});
		}
		return btnCancelar;
	}
}
